package com.kh.pajx.sop.view.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.kh.pajx.sop.base.ModelDrivenBaseAction;
import com.kh.pajx.sop.domain.HsSchoolInfo;

/**
 * Action控制器标准模版
 * 
 * @author: azzcsimp
 * @Createdate: 2014年6月12日 下午4:45:43
 */

@Controller("searchSchoolAction")
@Scope("prototype")
/**
 *查询学校列表 
 */
public class SearchSchoolAction extends ModelDrivenBaseAction<HsSchoolInfo> implements ServletRequestAware {
	private HttpServletRequest request;
	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}
	public String fromAreaCode(){
		Map<String, String> params = new HashMap<String, String>();
		String areaCode=request.getParameter("areaCode");
		List<HsSchoolInfo> schools=schoolService.searchSchool(params);
		request.setAttribute("message",initAreaCodeSchool(schools,areaCode));
		return "ajaxmsg";
	}
	public String fromNameSpell(){
		Map<String, String> params = new HashMap<String, String>();
		String schName=request.getParameter("keyword");
		List<HsSchoolInfo> schools=schoolService.searchSchool(params);
		request.setAttribute("message",initNameSpellSchool(schools,schName));
	    return "ajaxmsg";
	}
	public String initAreaCodeSchool(List<HsSchoolInfo> schools,String areaCode){
		List<HsSchoolInfo> results=new ArrayList<HsSchoolInfo>();
		if(areaCode!=null && !"0".equals(areaCode)){
			for(int i=0;i<schools.size();i++){
				HsSchoolInfo sch=schools.get(i);
				String schAreaCode=sch.getAreaCode();
				if(areaCode.equals(schAreaCode)){
					results.add(sch);
				}
			}
		}
	    StringBuilder sb=new StringBuilder("[");
	    for(int i=0;i<results.size();i++){
	    	  HsSchoolInfo sch=results.get(i);
	    	  String name=sch.getSclName().replaceAll("'", "").replaceAll("\"", "");
	    	  sb.append("{\"id\":\"").append(sch.getSclId()).append("\",\"value\":\"").append(name).append("\"}");
	    	  if(i<results.size()-1){
	    	       sb.append(",");
	    	  }
	    }
	    sb.append("]");	
	    return sb.toString();
}
	public String initNameSpellSchool(List<HsSchoolInfo> schools,String schName){
		if(schName!=null){
			schName=schName.trim().toLowerCase();
		}
		List<HsSchoolInfo> results=new ArrayList<HsSchoolInfo>();
		if(!"".equals(schName)){
			for(int i=0;i<schools.size();i++){
				HsSchoolInfo sch=schools.get(i);
				String name=sch.getSclName();
				String nameSpell=sch.getSclSpell();
				if(nameSpell!=null){
					name=name+nameSpell.toLowerCase();
				}
				if(name.indexOf(schName)>=0){
					results.add(sch);
				}
			}
		}
	    StringBuilder sb=new StringBuilder("[");
	    for(int i=0;i<results.size();i++){
	    	  HsSchoolInfo sch=results.get(i);
	    	  String name=sch.getSclName().replaceAll("'", "").replaceAll("\"", "");
	    	  sb.append("{\"id\":\"").append(sch.getSclId()).append("\",\"value\":\"").append(name).append("\"}");
	    	  if(i<results.size()-1){
	    	       sb.append(",");
	    	  }
	    }
	    sb.append("]");	
	    return sb.toString();
	}
}
